package com.ruoyi.bus.baidu;

import com.alibaba.fastjson.JSONObject;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import com.ruoyi.bus.qiniu.QiniuCloudUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @Description 百度文字转语音接口
 * @Author caiyanshan
 * @Date 2021/4/26 15:22
 * @Param
 * @Return
 */
public class BaiDuApi {
  public static final String APP_ID = "24028388";
  public static final String API_KEY = "iLjCsDN3Z8BOoRb5AyT69RNx";
  public static final String SECRET_KEY = "U4t0rihlObprW873clPx0GjoaGxSKbHG";
  
  public static String uploadSiteVoice(String siteName) {
    // 初始化一个AipSpeech
    AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
  
    // 可选：设置网络连接参数
    client.setConnectionTimeoutInMillis(2000);
    client.setSocketTimeoutInMillis(60000);
  
    // 调用接口
    TtsResponse res = client.synthesis(siteName, "zh", 1, null);
    byte[] data = res.getData();
    if (data != null) {
      try {
        //Util.writeBytesToFileSystem(data, "d:/output.mp3");
        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(data);
        JSONObject json = QiniuCloudUtil.upload(byteInputStream, "bus/commom/" + System.currentTimeMillis() + ".mp3");
        return json.getString("key");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
